const api = require('../../../utils/api.js')
import { $wuxToast } from '../../../dist/index'
let comId;
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    adminRes:false,
    isDianzanCom:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var question = JSON.parse(options.question)
    console.log(question.comment)
    this.setData({
      question: question
    })
    if(question.comment!=null){
      this.setData({
        adminRes : true
      })
    }
    this.isDianzan(question.id)
    this.getComment(question.id)
    // this.getAdminComment(question)
  },

  
  dianzanComFun(e){
    var that = this  
    comId = e.currentTarget.dataset.id;
    console.log(1)
        wx.request({
          url: api.addLike,
          method:"POST",
          data:{
            id: comId
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzanCom:true
              }) 
            }
          }
        }) 
  },
  cancelComDianzan(e){
    var that = this
    comId = e.currentTarget.dataset.id;
    console.log(comId)
    wx.request({
      url: api.removeLike,
      method:"POST",
      data:{
        id: comId
      },
      success(res){
        if(res.data.errno == 0){
          that.setData({
            isDianzanCom:false
          })
          
        }
      }
    })
  },
  isDianzan(id){
    var that = this
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.isDianzan,
          method:"POST",
          data:{
            userId: res.data,
            questionId: id
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan:true
              })
            }
          }
        })
      }
    })
  },
  cancelDianzan(){
    var that = this
    var question = this.data.question
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.cancelDianzan,
          method:"POST",
          data:{
            questionId: question.id,
            userId: res.data
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan: false
              })
              that.showToast("取消点赞")
            }
          }
        })
      }
    })
  },
  dianzanFun(){
    var question = this.data.question
    var that = this
    wx.getStorage({
      key: 'userId',
      success(res){
        wx.request({
          url: api.addDianzan,
          method:"POST",
          data:{
            userId: res.data,
            questionId: question.id
          },
          success(res){
            if(res.data.errno == 0){
              that.setData({
                isDianzan: true
              })
              that.showToast("点赞成功")
            }
          }
        })
      }
    })
  },
  showToast(info) {
    $wuxToast().show({
        type: 'success',
        duration: 1500,
        color: '#fff',
        text: info,
        success: () => {}
    })
  },
  onChange(){
    var questionId = this.data.question.id
    wx.navigateTo({
      url: '../../comment/comment?questionId='+questionId,
    })
  }, 
  // getAdminComment(id){
  //   var that = this
  //   //console.log(id)
  //   wx.request({
  //     url: api.adminComment,
  //     data:{
  //       id: id.id
  //     },
  //     success(res){
  //       if(res.data.data.length > 0){
  //         console.log(res.data.data)
  //         that.setData({
  //           comment1: res.data.data.comment,
  //           adminRes: true
  //         })
  //       }

  //     }
  //   })
  // },
  getComment(id){
    var that = this
    wx.request({
      url: api.getQuestionComment,
      data:{
        questionId: id
      },
      success(res){
        if(res.data.data.length > 0){
          //console.log(res.data.data)
          that.setData({
            comment: res.data.data,
            show: true
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var question = this.data.question
    this.getComment(question.id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})